﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using AutoFacUtils;
using CashFlow.ShopManage.Service;

namespace CashFlow.ShopManage.Filters
{
    public class LoginAuthorizeFilterAttribute : AuthorizeAttribute
    {
        private static ILog _logger = LogManager.GetLogger(typeof(LoginAuthorizeFilterAttribute));
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var service = GetAdminService();
            if (service.IsLogin())
            {
                return true;
            }

            if (httpContext.Request.Headers.AllKeys.Contains("X-Requested-With") &&
                httpContext.Request.Headers["X-Requested-With"].ToString().Trim().Equals("XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
            {
                httpContext.Response.ClearContent();
                httpContext.Response.AddHeader("sessionTimeout", "true");
                httpContext.Response.End();
            }

            return false;
        }
    }
}